package com.chuanye.mydrawable.adapters;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanye.mydrawable.R;
import com.chuanye.mydrawable.activitys.RemoteActivity;
import com.chuanye.mydrawable.beans.SupportBean;

import java.util.HashMap;
import java.util.List;

/**
 * Created by lsef on 2019/2/13.
 */

public class SupportAdapter extends BaseAdapter{
    private Context mContext;
    private List<SupportBean.ObjBean> objBeanList;
    private List<SupportBean.ObjBean> objBeanList2;
    // 用来控制CheckBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;
    private int one;

    public SupportAdapter(Context mContext, List<SupportBean.ObjBean> objBeanList,HashMap<Integer,Boolean> isSelected,List<SupportBean.ObjBean> objBeanList2,int one) {
        this.mContext = mContext;
        this.objBeanList = objBeanList;
        this.isSelected = isSelected;
        this.objBeanList2 = objBeanList2;
        this.one = one;
        // 初始化数据
        initDate();
    }

    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < objBeanList.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    @Override
    public int getCount() {
        return objBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return objBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null){
            view = View.inflate(mContext, R.layout.support_itme,null);
            holder = new ViewHolder();
            holder.support_item_tv = view.findViewById(R.id.support_item_tv);
            holder.support_item_iv = view.findViewById(R.id.support_item_iv);
            holder.support_item_ll = view.findViewById(R.id.support_item_ll);
            view.setTag(holder);
        }
        holder = (ViewHolder) view.getTag();
        holder.support_item_tv.setText(objBeanList.get(i).getSupportName());

        if (objBeanList2 != null && objBeanList2.size() > 0){
            for (int f = 0; f < objBeanList2.size(); f ++){
                Log.i("打印：",objBeanList2.get(f).getSupportName());
                if (objBeanList2.get(f).getSupportName().equals(objBeanList.get(i).getSupportName())){
                    isSelected.put(i, true);
                }else {
                    //isSelected.put(i, false);
                }
            }
        }

         // 监听checkBox并根据原来的状态来设置新的状态
        final ViewHolder finalHolder = holder;
        holder.support_item_ll.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //Log.i("点击了333：",""+isSelected.get(i));
                if (isSelected.get(i)) {
                    //Log.i("点击了344：",""+isSelected.get(i));
                    isSelected.put(i, false);
                    setIsSelected(isSelected);
                    if (one == 1){//远程外部
                        if (RemoteActivity.supportList2 !=null && RemoteActivity.supportList2.size() > 0){
                            for(int k = 0 ; k < RemoteActivity.supportList2.size(); k++){
                                if (RemoteActivity.supportList2.get(k).getSupportName().equals(objBeanList.get(i).getSupportName())){
                                    RemoteActivity.supportList2.remove(k);
                                    RemoteActivity.recyclerViewAdapter.notifyDataSetChanged();
                                }
                            }
                        }
                    }else if (one == 2){//现场外部
                        if (RemoteActivity.spotexternal !=null && RemoteActivity.spotexternal.size() > 0){
                            for(int k = 0 ; k < RemoteActivity.spotexternal.size(); k++){
                                if (RemoteActivity.spotexternal.get(k).getSupportName().equals(objBeanList.get(i).getSupportName())){
                                    RemoteActivity.spotexternal.remove(k);
                                    RemoteActivity.spotexternalAdapter.notifyDataSetChanged();
                                }
                            }
                        }

                    }

                } else {
                    isSelected.put(i, true);
                    setIsSelected(isSelected);
                    //Log.i("yyyy","i="+i);
                    if (one == 1){//远程外部
                        if(objBeanList.get(i).getSupportId() == 999){//如果选择无 去掉其它
                            RemoteActivity.supportList2.clear();//listview
                            for (int y =0; y < isSelected.size(); y++){//popwindow
                                if (y != i){
                                    isSelected.put(y, false);
                                    setIsSelected(isSelected);
                                }
                            }
                        }else {
                            for (int y =0; y < objBeanList.size(); y++){//popwindow
                                if (objBeanList.get(y).getSupportId() == 999){
                                    isSelected.put(y, false);
                                    setIsSelected(isSelected);
                                    notifyDataSetChanged();
                                }
                            }
                            if (RemoteActivity.supportList2 !=null && RemoteActivity.supportList2.size() > 0){//listview
                                for(int k = 0 ; k < RemoteActivity.supportList2.size(); k++){
                                    if (RemoteActivity.supportList2.get(k).getSupportId() == 999){
                                        RemoteActivity.supportList2.remove(k);
                                    }
                                }
                            }
                        }
                        RemoteActivity.supportList2.add(objBeanList.get(i));
                        RemoteActivity.recyclerViewAdapter.notifyDataSetChanged();
                        RemoteActivity.crreremot_remoteexternal_tv.setVisibility(View.GONE);
                        RemoteActivity.crreremot_remoteexternal_rv.setVisibility(View.VISIBLE);
                    }else if (one == 2){//现场外部

                        if(objBeanList.get(i).getSupportId() == 999){//如果选择无 去掉其它
                            RemoteActivity.spotexternal.clear();//listview
                            for (int y =0; y < isSelected.size(); y++){//popwindow
                                if (y != i){
                                    isSelected.put(y, false);
                                    setIsSelected(isSelected);
                                }
                            }
                        }else {
                            for (int y =0; y < objBeanList.size(); y++){//popwindow
                                if (objBeanList.get(y).getSupportId() == 999){
                                    isSelected.put(y, false);
                                    setIsSelected(isSelected);
                                    notifyDataSetChanged();
                                }
                            }
                            if (RemoteActivity.spotexternal !=null && RemoteActivity.spotexternal.size() > 0){//listview
                                for(int k = 0 ; k < RemoteActivity.spotexternal.size(); k++){
                                    if (RemoteActivity.spotexternal.get(k).getSupportId() == 999){
                                        RemoteActivity.spotexternal.remove(k);
                                    }
                                }
                            }
                        }
                        RemoteActivity.spotexternal.add(objBeanList.get(i));
                        RemoteActivity.spotexternalAdapter.notifyDataSetChanged();
                        RemoteActivity.crreremot_spotexternal_tv.setVisibility(View.GONE);
                        RemoteActivity.crreremot_spotexternal_rv.setVisibility(View.VISIBLE);
                    }

                }
                notifyDataSetChanged();
            }
        });
       // 根据isSelected来设置checkbox的选中状况
        if (getIsSelected().get(i)){
            holder.support_item_iv.setVisibility(View.VISIBLE);
        }else {
            holder.support_item_iv.setVisibility(View.GONE);
        }

        return view;
    }

    public static class ViewHolder{
        TextView support_item_tv;
        ImageView support_item_iv;
        LinearLayout support_item_ll;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        SupportAdapter.isSelected = isSelected;
    }
}
